﻿using System;
using System.Globalization;
using System.Web.UI;
using ServiceClient;
using WebMvc.Models;

namespace Web
{
    public partial class Default : Page
    {
        protected OpenMeetingsServiceClient Service
        {
            get
            {
                if (Session["service"] == null)
                    Session["service"] = new OpenMeetingsServiceClient();

                return (OpenMeetingsServiceClient)Session["service"];
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnObtenerToken_Click(object sender, EventArgs e)
        {
            //txtUsr.Text = string.Empty;
            //txtPass.Text = string.Empty;
            //litResultadoLogin.Text = string.Empty;

            litCurrentToken.Text = Service.CurrentToken;
        }

        //protected void BtnLoginClick(object sender, EventArgs e)
        //{
        //    string errMsg;

        //    litResultadoLogin.Text = Service.Login(txtUsr.Text, txtPass.Text, out errMsg)
        //        ? "Login OK."
        //        : errMsg;
        //}

        protected void BtnCrearSala_Click(object sender, EventArgs e)
        {
            try
            {
                Service.CreateNewRoom(txtNombreSala.Text, int.Parse(ddlTipo.SelectedValue), txtComentarioSala.Text);

                var random = new Random();
                var externalRoomId = random.Next();

                var roomId = Service.GetRoomId(txtNombreSala.Text, int.Parse(ddlTipo.SelectedValue), txtComentarioSala.Text, externalRoomId);
                
                litExternalRoomId.Text = externalRoomId.ToString();
                litIdSala.Text = roomId.ToString();
                litUrlSala.Text = Service.GetRoomUrl(roomId);
            }
            catch (Exception ex)
            {
                MostrarError(ex.Message);
            }
        }

        protected void MostrarError(string mensaje)
        {
            pnlError.Visible = true;
            litError.Text = mensaje;
        }

        protected void BtnCrearEvento_Click(object sender, EventArgs e)
        {
            try
            {
                var roomType = int.Parse(ddlTipo.SelectedValue);
                var roomId = int.Parse(litIdSala.Text);
                var repeticion = (RepeticionEvento)int.Parse(rblSeRepite.SelectedValue);


                var formatosFecha = new[] { "dd/MM/yyyy HH:mm:ss", "dd/MM/yyyy HH:mm", "dd/MM/yy HH:mm", "dd/MM/yy HH:mm:ss" };

                var fechaInicio = DateTime.ParseExact(txtFechaInicio.Text, formatosFecha, CultureInfo.InvariantCulture,
                    DateTimeStyles.None);

                var fechaFin = DateTime.ParseExact(txtFechaFin.Text, formatosFecha, CultureInfo.InvariantCulture,
                    DateTimeStyles.None);

                var result = Service.CreateNewAppointment(roomId, txtNombreEvento.Text, txtLugarEvento.Text,
                                txtDescripcionEvento.Text, repeticion,
                                fechaInicio, fechaFin, roomType);

                litIdEvento.Text = result.ToString();
            }
            catch (Exception ex)
            {
                MostrarError(ex.Message);
            }
        }
    }
}
